﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
  public  class CBizShpOrderRate: CustBizObject
    {
        private CDmShpOrderRate m_CDmShpOrderRate = new CDmShpOrderRate();
        private CDmShpOrderRate oDmShpOrderRate
        {
            get { return m_CDmShpOrderRate; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizShpOrderRate()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmShpOrderRate.SHP_ORDER_RATE);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ORDER_LIST_ID"]; 
            //this.DataObj.IdGroup = "RID";
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            } 

            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }

        public DataSet SelectByOrderId(string strOrderId)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  ORDER_FORM_ID =  '" + strOrderId + "' ORDER BY ORDER_LIST_ID ASC");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }


        public DataSet SelectForRateShowByOrderId(string strOrderId)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT A.*,B.IMG_URL,B.GOODS_NME FROM " + this.TableName + " A LEFT OUTER JOIN SHP_ORDER_FORM_LIST B ON A.ORDER_LIST_ID=B.ORDER_LIST_ID WHERE  A.ORDER_FORM_ID =  '" + strOrderId + "' ORDER BY B.SEQ_NUM ASC");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectRateStatistic(string strGoodsId)
        {
            DataSet ds = this.DataObj.QueryBySQL(@"
    SELECT    
     ZUIP_COUNT =SUM(CASE WHEN A.BUYER_RATE_DT2 IS NOT NULL THEN 1 ELSE 0 END)
    ,IMG_COUNT=SUM(CASE WHEN A.GOODS_IMG_URL1 IS NOT NULL AND A.GOODS_IMG_URL1<>'' THEN 1 ELSE 0 END)
    ,HAOP_COUNT=SUM(CASE WHEN A.BUYER_RATE_NUM=1 THEN 1 ELSE 0 END)
    ,ZHONGP_COUNT =SUM(CASE WHEN A.BUYER_RATE_NUM=0 THEN 1 ELSE 0 END)
    ,CHAP_COUNT =SUM(CASE WHEN A.BUYER_RATE_NUM=-1 THEN 1 ELSE 0 END)
    FROM SHP_ORDER_RATE A
    WHERE
    (A.GOODS_ID='" + strGoodsId + "') ");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }
       

    }
}
